package com.wnxy.config;

import com.wnxy.interceptors.JwtInterceptor;
import com.wnxy.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    JwtInterceptor jwtInterceptor;

    @Autowired
    LoginInterceptor loginInterceptor;


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        //配置  访问urlPath时对应的视图模板
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");

        registry.addViewController("/weaponType.html").setViewName("weaponType");

        registry.addViewController("/singleWeapon.html").setViewName("singleWeapon");

        registry.addViewController("/register.html").setViewName("register");

        registry.addViewController("/login.html").setViewName("login");

        registry.addViewController("/myCart.html").setViewName("myCart");

        registry.addViewController("/orderPreview.html").setViewName("orderPreview");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //拦截器按照注册的先后顺序执行

        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**");

        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/myCart.html")
                .addPathPatterns("/cart/**")
        ;

    }
}
